package com.xgkj.diyiketang.task;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.xgkj.diyiketang.ProtocolConst;

import com.xgkj.diyiketang.data.ShangQuanInfo;
import com.xgkj.diyiketang.http.CouponHttpRequest;
import com.xgkj.diyiketang.http.CouponHttpResponse;
import com.xgkj.diyiketang.http.engine.CouponHttpBaseTask;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrater on 2015/12/7.
 */
public class ShangQuanListTask extends CouponHttpBaseTask {

    private String mAreaName;
    private static final String MOREVIDEOLIST = "tradingAreaList";

    public ShangQuanListTask( String areaName) {
        super(TaskType.SHANGQUANLIST_TASK);
       mAreaName = areaName;
    }

    @Override
    protected void processResponseData(CouponHttpResponse response) {
        JSONObject json = response.getBody();
        int currentPage = 0;
        int pageCount = 0;
        int nextPage = 0;
        List<ShangQuanInfo> list = new ArrayList<ShangQuanInfo>();
//        if (json != null) {
//            JSONObject jsonString = json.getJSONObject(MOREVIDEOLIST);
//            if (jsonString != null) {
//                currentPage = jsonString
//                        .getIntValue(ProtocolConst.CURRENT_PAGE);
//                pageCount = jsonString.getIntValue(ProtocolConst.PAGE_COUNT);
//                nextPage = currentPage >= pageCount ? 0 : (currentPage + 1);
                list = JSON.parseArray(
                        json.getString(ProtocolConst.LIST_TAG),
                        ShangQuanInfo.class);
//            }

//        }
        notifyTaskCompleted(nextPage, list);
    }

    @Override
    protected void prepareRequest() {
        super.prepareRequest();
        if (mPhase == TASK_PHASE_INIT) {
            CouponHttpRequest request = new CouponHttpRequest(
                    RequestType.SHANGQUANLIST_REQUEST);
            JSONObject json = new JSONObject();
            json.put("areaName", mAreaName);

            request.setData(json.toString());
            request.setApiNo(ApiNo.SHANGQUANLIST);
            addRequest(request);
        }
    }
}
